#include "stdafx.h"
#include "LogFile.h"


#pragma region Constructor
/// <summary>
/// Initialize a default instance of the class.
/// </summary>
LogFile::LogFile()
{
	this->fileName = NULL;
	this->handle = NULL;
	this->tagReadings = NULL;
	this->tagSensor = NULL;
}
#pragma endregion

#pragma region Destructor
/// <summary>
/// Free allocated memory.
/// </summary>
LogFile::~LogFile()
{
	// clear the file name
	if (this->fileName != NULL)
	{
		delete[] this->fileName;
		this->fileName = NULL;
	}

	// delete the logtag data holders
	if (this->tagReadings != NULL)
	{
		delete[] this->tagReadings;
		this->tagReadings = NULL;
	}

	if (this->tagSensor != NULL)
	{
        delete[] this->tagSensor;
		this->tagSensor = NULL;
	}

	// close the logtag handle
	if (this->handle != NULL)
	{
		LogTag_Close(this->handle);
		this->handle = NULL;
	}

}
#pragma endregion

#pragma region Set File Name
/// <summary>
/// Set the file name.
/// </summary>
void LogFile::SetFileName(const char *fileName)
{
	int length = strlen(fileName);
	this->fileName = new char[length + 1];
	strcpy(this->fileName, fileName);
}
#pragma endregion

#pragma region Get File Name
/// <summary>
/// Get the file name.
/// <summary>
char const *LogFile::GetFileName()
{
	return this->fileName;
}
#pragma endregion

#pragma region Open File
/// <summary>
/// Attempt to open the logtag file.
/// </summary>
bool LogFile::OpenFile()
{
	if (this->fileName != NULL)
	{
		// open access to logtag
		LOGTAG_HANDLE handle = LogTag_OpenAccess(0);
		if (handle == NULL) 
		{
			if (LogTag_LogOnUser(NULL, NULL, NULL) != LTR_SUCCESS)
				return false;

			handle = LogTag_OpenAccess(0);
			if (handle == NULL)
				return false;
		}

		// set the handle
		this->handle = handle;

		// attempt to open the file
		return (LogTag_OpenPath(handle, (LPCTSTR)this->fileName) == LTR_SUCCESS);
	}

	return false;
}
#pragma endregion

#pragma region Load Data
/// <summary>
/// Load the LogTag data
/// </summary>
bool LogFile::LoadData()
{
	// create the tag interface stracture
	memset(&this->tagInterface, 0, sizeof(this->tagInterface));
	this->tagInterface.cbSize = sizeof(this->tagInterface);
	
	// create the tag info structure
	memset(&this->tagInfo, 0, sizeof(this->tagInfo));
	this->tagInfo.cbSize = sizeof(this->tagInfo);

	// clear the readings and sensor structures
	if (this->tagReadings != NULL)
	{
		delete[] this->tagReadings;
		this->tagReadings = NULL;
	}

	if (this->tagSensor != NULL)
	{
		delete[] this->tagSensor;
		this->tagSensor = NULL;
	}

	// attempt to load the data
	if (LogTag_GetInterface(this->handle, &this->tagInterface) == LTR_SUCCESS)
		if ( (this->tagInterface.wStatus & LTIS_LOGTAG) == LTIS_LOGTAG)
			if (LogTag_GetData(handle, &this->tagInfo) == LTR_SUCCESS)
				if (this->tagInfo.dwNumOfReadings != 0)
				{
					this->tagSensor = new LOGTAG_SENSOR[this->tagInfo.wNumOfSensors];
					this->tagInfo.pSensor = this->tagSensor;
					this->tagInfo.wSensorCount = this->tagSensor ? this->tagInfo.wNumOfSensors : 0;

					for (WORD nSensor = 0; nSensor != this->tagInfo.wSensorCount; nSensor++)
						this->tagSensor[nSensor].cbSize = sizeof(LOGTAG_SENSOR);

					this->tagReadings = new LOGTAG_READING[this->tagInfo.dwNumOfReadings];
					this->tagInfo.pReading = this->tagReadings;
					this->tagInfo.dwReadingsCount = this->tagReadings ? this->tagInfo.dwNumOfReadings : 0;
					
					if (LogTag_GetData(handle, &this->tagInfo) != LTR_SUCCESS)
						this->tagInfo.dwNumOfReadings = 0;

					return true;
				}

		return false;
}
#pragma endregion

#pragma region Get Reading Count
/// <summary>
/// Get the number of readings
/// </summary>
long LogFile::GetReadingCount()
{
	if (this->handle != NULL)
	{
		return this->tagInfo.dwReadingsCount;
	}

	return -1;
}
#pragma endregion


LOGTAG_READING LogFile::GetReadingByIndex(long index)
{
	LOGTAG_READING result;

	long count = this->GetReadingCount();
	if ( (count > 0) && (index < count) )
	{
		result = (this->tagReadings[index]);
	}

	return result;
}
